﻿layui.use(['table', 'laypage', 'layer'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var layer = layui.layer;
    var $ = layui.$;

    //绑定品种的枚举
    $(document).ready(function () {
        $.ajax({
            url: 'http://10.31.60.18:8000/BreedingsGetRout/api/PhaseDetermination/zhuanSheepBreed', // 后端枚举数据接口地址
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var enumData = res; // 直接使用返回的数据
                var select = $('#sheepBreed');
                $.each(enumData, function (index, item) {
                    // 使用正确的字段添加选项到下拉框中
                    var option = $('<option>').val(item.txt).text(item.txt);
                    select.append(option);
                });
                // 手动触发 layui 组件的渲染
                layui.form.render('select');
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });
   

    var pageSize = 15; // 每页显示条数
    //获取列表
    function getListData(pageIndex) {
        //获取查询条件
        var earNumber = $('#earNumber').val();
        var sheepBreed = $('#sheepBreed').val();
        var sheepGender = $('#sheepGender').val();
        //ajax后台交互
        $.ajax({
            url: 'http://10.31.60.18:8000/BreedingsGetRout/api/PhaseDetermination/ShowPhaseDetermination',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                earNumber: earNumber,
                sheepBreed: sheepBreed,
                sheepGender: sheepGender ,
            },
            success: function (res) {
                table.render({
                    //挂载的表格id
                    elem: '#ID-table-demo-setRowChecked',
                    cols: [
                        [
                            { type: 'checkbox' },
                            { field: 'earNumber', title: '耳号', width: 95 },
                            {
                                field: 'sheepBreed', title: '品种', align: 'center', width: 95
                            },
                            {
                                field: 'sheepGender', title: '性别', align: 'center', width: 95,
                                templet: function (data) {
                                    return data.sheepGender == true ? "公" : "母"
                                }
                            },

                            { field: 'cottageId', title: '当前栋舍', width: 95 },
                            { field: 'field', title: '当前栏位', width: 95 },
                            { field: 'measureDate', title: '测定日期', templet: function (data) { return data.measureDate.substring(0, 10) }, width: 95 },
                            { field: 'ageDays', title: '测定日龄', width: 95 },
                            {
                                field: 'toothOcclusion', title: '牙齿咬合', align: 'center', width: 95,
                                templet: function (data) {
                                    if (data.toothOcclusion == 1) return '正常';
                                    if (data.toothOcclusion == 2) return '错齿';
                                }
                            },
                            { field: 'hair', title: '毛发', width: 95 },
                            { field: 'stain', title: '色斑', width: 95 },
                            { field: 'hoofColor', title: '蹄颜色', width: 95 },
                            { field: 'mouthColor', title: '嘴颜色', width: 95 },
                            {
                                field: 'angle', title: '角', align: 'center', width: 95,
                                templet: function (data) {
                                    if (data.angle == 1) return '有角';
                                    if (data.angle == 2) return '无角';
                                }
                            },
                            { field: 'fatCistribution', title: '脂肪分布', width: 95 },
                            { field: 'createBy', title: '创建人', width: 95 },
                            { field: 'createDate', title: '创建时间', width: 95 },
                            { field: 'updateBy', title: '修改人', width: 95 },
                            { field: 'updateDate', title: '修改时间', width: 95 },
                            { title: '操作', toolbar: '#barDemo', width: 95 }
                        ]
                    ],
                    data: res.datas
                });

                //分页
                laypage.render({
                    //挂载的分页id
                    elem: 'pagesbox',
                    count: res.totalCount,
                    limit: pageSize,
                    curr: pageIndex,
                    layout: ['count', 'prev', 'page', 'next', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            getListData(obj.curr);
                        }
                    }
                });
            }
        });
    }

    $('#searchBtn').on('click', function () {
        getListData(1);
    });
    // 单删除
    function deleteData(phaseDeterminationId) {
        $.ajax({
            url: `http://10.31.60.18:8000/BreedingsSetRout/api/PhaseDetermination/DeletePhaseDetermination?PhaseDeterminationId=${phaseDeterminationId}`,
            type: 'delete',
            success: function (res) {
                if (res.code == 200) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除失败');
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }
    // 批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
            data = checkStatus.data,
            phaseDeterminationId = "";
        if (data.length > 0) {
            for (var i in data) {
                phaseDeterminationId += data[i].phaseDeterminationId + ",";
            }
            phaseDeterminationId = phaseDeterminationId.substring(0, phaseDeterminationId.length - 1);
            console.log(phaseDeterminationId);
            layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: `http://10.31.60.18:8000/BreedingsSetRout/api/PhaseDetermination/BatchDeletePhaseDetermination?phaseDeterminationId=${phaseDeterminationId}`,
                    type: 'delete',
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg("批量删除成功");
                            getListData(1);
                        } else {
                            layer.msg("批量删除失败");
                        }
                    },
                    error: function () {
                        layer.msg('批量删除请求失败');
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg("请选择需要删除的记录");
        }
        console.log(checkStatus);
    });

    // 监听工具条，实现删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        var PhaseDeterminationId = data.phaseDeterminationId;
        if (obj.event == 'edit') {
            var url = `/Breedings/UpdatePhaseDetermination?PhaseDeterminationId=${PhaseDeterminationId}`;
            location.href = url; // 点击编辑按钮时跳转到修改页面
        }

        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.phaseDeterminationId); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });

    $(function () {
        // 初始化加载数据
        getListData(1);
    })
})